package org.zhangchao.poi.excel.process;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Row;
import org.zhangchao.poi.excel.common.ExcelEnum;
import org.zhangchao.poi.excel.common.template.ExcelTemplateParam;
import org.zhangchao.poi.excel.interceptor.ExcelHandlerInterceptor;
import org.zhangchao.poi.excel.interceptor.ExcelHandlerInterceptorStack;
import org.zhangchao.poi.excel.interceptor.MakeHandler;

/**
 * Excel 基本解析器,需要配置拦截器使用
 * 
 * @author zhangchao
 * 
 */
public class ExcelBaseProcessor<E> extends ExcelProcessor implements MakeHandler {

	/** 拦截器栈 */
	private ExcelHandlerInterceptorStack handlerStack;

	/** 数据集合 */
	private List<E> data = new ArrayList<E>();

	/**
	 * 构建解析器
	 * 
	 * @param fileName
	 * @throws Exception
	 */
	public ExcelBaseProcessor(String fileName) throws Exception {
		super(fileName);
	}
	
	/**
	 * 构建解析器
	 * 
	 * @param file
	 * @throws Exception
	 */
	public ExcelBaseProcessor(File file) throws Exception {
		super(file);
	}

	/**
	 * 构建解析器
	 * 
	 * @param file
	 * @param type
	 * @throws Exception
	 */
	public ExcelBaseProcessor(File file, ExcelEnum type) throws Exception {
		super(file, type);
	}

	/**
	 * 构建解析器
	 * 
	 * @param is
	 * @param type
	 * @throws Exception
	 */
	public ExcelBaseProcessor(InputStream is, ExcelEnum type) throws Exception {
		super(is, type);
	}

	/**
	 * 构建解析器
	 * 
	 * @param fileName
	 * @param handlerStack
	 * @throws Exception
	 */
	public ExcelBaseProcessor(String fileName,
			ExcelHandlerInterceptorStack handlerStack) throws Exception {
		super(fileName);
		this.handlerStack = handlerStack;
	}
	
	/**
	 * 构建解析器
	 * 
	 * @param file
	 * @param handlerStack
	 * @throws Exception
	 */
	public ExcelBaseProcessor(File file,
			ExcelHandlerInterceptorStack handlerStack) throws Exception {
		super(file);
		this.handlerStack = handlerStack;
	}

	/**
	 * 构建解析器
	 * 
	 * @param file
	 * @param type
	 * @param handlerStack
	 * @throws Exception
	 */
	public ExcelBaseProcessor(File file,ExcelEnum type,
			ExcelHandlerInterceptorStack handlerStack) throws Exception {
		super(file, type);
		this.handlerStack = handlerStack;
	}

	/**
	 * 构建解析器
	 * 
	 * @param is
	 * @param type
	 *            ExcelProcessor.TYPE2003 or ExcelProcessor.TYPE2007
	 * @param handlerStack
	 * @throws Exception
	 */
	public ExcelBaseProcessor(InputStream is, ExcelEnum type,
			ExcelHandlerInterceptorStack handlerStack) throws Exception {
		super(is, type);
		this.handlerStack = handlerStack;
	}

	@Override
	public void processRow(Row row) {
		if (null == handlerStack) {
			return;
		}
		handlerStack.handler(row, this, handlerStack);
	}
	
	@Override
	public List<?> prepareModelData() {
		return getData();
	}
	
	/**
	 * 添加拦截器
	 * @param excelHandlerInterceptor
	 */
	public void addExcelHandleerInterceptor(ExcelHandlerInterceptor excelHandlerInterceptor ) {
		handlerStack.add(excelHandlerInterceptor);
	}

	public ExcelHandlerInterceptorStack getHandlerStack() {
		return handlerStack;
	}

	public void setHandlerStack(ExcelHandlerInterceptorStack handlerStack) {
		this.handlerStack = handlerStack;
	}

	public List<E> getData() {
		return data;
	}

	public void setData(List<E> data) {
		this.data = data;
	}

	//@Override
	public ExcelTemplateParam getExcelTemplateaParam() {
		return null;
	}

}
